package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.ShortAnswer;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.ShortAnswerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/17 - 23:30
 */
@RestController
public class ShortAnswerController {

    @Autowired
    private ShortAnswerService shortAnswerService;

    @RequestMapping("/sa/all")
    public List<ShortAnswer> getAll(){
        return shortAnswerService.selectAll();
    }



    @RequestMapping("/sa/add")
    public ResponseEntity add(@RequestBody ShortAnswer shortAnswer){
        shortAnswer.setCreateDate(TimeUtils.getNowTime());
        int res = shortAnswerService.insert(shortAnswer);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @GetMapping("/sa/find/{id}")
    public ShortAnswer find(@PathVariable("id") int id){
        return shortAnswerService.selectByPrimaryKey(id);
    }

    @RequestMapping("/sa/search")
    public List<ShortAnswer> search(QuestionDto questionDto){
        ShortAnswer shortAnswer = new ShortAnswer();
        BeanUtils.copyProperties(questionDto,shortAnswer);
        return shortAnswerService.selectByAll(shortAnswer);
    }

    @PostMapping("/sa/caps")
    public ResponseEntity conditionAndPageSearch(QuestionDto questionDto){
        ShortAnswer shortAnswer = new ShortAnswer();
        BeanUtils.copyProperties(questionDto,shortAnswer);
        PageHelper.startPage(questionDto.getPageNum(),questionDto.getPageSize());
        List<ShortAnswer> list =  shortAnswerService.selectByAll(shortAnswer);
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @RequestMapping("/sa/update")
    public ResponseEntity update(@RequestBody ShortAnswer shortAnswer){
        int res = shortAnswerService.updateByPrimaryKeySelective(shortAnswer);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @RequestMapping("/sa/delete")
    public ResponseEntity delete(int id){
        int res = shortAnswerService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }
    
}
